<template>
  <Dialog v-model="dialogVisible" title="订单详情" width="60%" class="enhanced-dialog">
    <!-- 弹窗头部 -->
    <div class="dialog-header">
      <div class="header-icon">
        <Icon icon="ep:document" />
      </div>
      <div class="header-content">
        <h4 class="dialog-title">订单详情</h4>
        <p class="dialog-subtitle">查看支付订单的详细信息和状态</p>
      </div>
    </div>

    <!-- 详情内容 -->
    <div class="detail-container">
      <!-- 基本信息 -->
      <div class="detail-section">
        <div class="section-title">
          <Icon icon="ep:info-filled" class="section-icon" />
          <span>基本信息</span>
        </div>

        <el-descriptions :column="2" label-class-name="desc-label" class="enhanced-descriptions">
          <el-descriptions-item label="商户单号">
            <div class="desc-value">
              <Icon icon="ep:document" class="value-icon" />
              <el-tag size="small" class="merchant-tag">{{ detailData.merchantOrderId }}</el-tag>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="支付单号">
            <div class="desc-value">
              <Icon icon="ep:key" class="value-icon" />
              <el-tag type="warning" size="small" v-if="detailData.no" class="pay-tag">{{
                detailData.no
              }}</el-tag>
              <span v-else class="no-data">-</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="应用编号">
            <div class="desc-value">
              <Icon icon="ep:document" class="value-icon" />
              <span class="value-text">{{ detailData.appId }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="应用名称">
            <div class="desc-value">
              <Icon icon="ep:document" class="value-icon" />
              <span class="value-text">{{ detailData.appName }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="支付状态">
            <div class="desc-value">
              <Icon icon="ep:switch" class="value-icon" />
              <dict-tag
                :type="DICT_TYPE.PAY_ORDER_STATUS"
                :value="detailData.status"
                size="small"
                class="status-tag"
              />
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="支付金额">
            <div class="desc-value">
              <Icon icon="ep:money" class="value-icon price-icon" />
              <el-tag type="success" size="small" class="price-tag"
                >￥{{ (detailData.price / 100.0).toFixed(2) }}</el-tag
              >
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="手续费">
            <div class="desc-value">
              <Icon icon="ep:coin" class="value-icon fee-icon" />
              <el-tag type="warning" size="small" class="fee-tag">
                ￥{{ (detailData.channelFeePrice / 100.0).toFixed(2) }}
              </el-tag>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="手续费比例">
            <div class="desc-value">
              <Icon icon="ep:percentage" class="value-icon" />
              <span class="value-text">{{ (detailData.channelFeeRate / 100.0).toFixed(2) }}%</span>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 时间信息 -->
      <div class="detail-section">
        <div class="section-title">
          <Icon icon="ep:clock" class="section-icon" />
          <span>时间信息</span>
        </div>

        <el-descriptions :column="2" label-class-name="desc-label" class="enhanced-descriptions">
          <el-descriptions-item label="支付时间">
            <div class="desc-value">
              <Icon icon="ep:check" class="value-icon success-icon" />
              <span class="value-text time-text">{{
                formatDate(detailData.successTime) || '-'
              }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="失效时间">
            <div class="desc-value">
              <Icon icon="ep:warning" class="value-icon warning-icon" />
              <span class="value-text time-text">{{
                formatDate(detailData.expireTime) || '-'
              }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="创建时间">
            <div class="desc-value">
              <Icon icon="ep:plus" class="value-icon" />
              <span class="value-text time-text">{{
                formatDate(detailData.createTime) || '-'
              }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="更新时间">
            <div class="desc-value">
              <Icon icon="ep:edit" class="value-icon" />
              <span class="value-text time-text">{{
                formatDate(detailData.updateTime) || '-'
              }}</span>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 商品信息 -->
      <div class="detail-section">
        <div class="section-title">
          <Icon icon="ep:goods" class="section-icon" />
          <span>商品信息</span>
        </div>

        <el-descriptions :column="2" label-class-name="desc-label" class="enhanced-descriptions">
          <el-descriptions-item label="商品标题">
            <div class="desc-value">
              <Icon icon="ep:goods" class="value-icon" />
              <span class="value-text">{{ detailData.subject || '-' }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="商品描述">
            <div class="desc-value">
              <Icon icon="ep:document" class="value-icon" />
              <span class="value-text">{{ detailData.body || '-' }}</span>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 渠道信息 -->
      <div class="detail-section">
        <div class="section-title">
          <Icon icon="ep:credit-card" class="section-icon" />
          <span>渠道信息</span>
        </div>

        <el-descriptions :column="2" label-class-name="desc-label" class="enhanced-descriptions">
          <el-descriptions-item label="支付渠道">
            <div class="desc-value">
              <Icon icon="ep:credit-card" class="value-icon" />
              <dict-tag
                :type="DICT_TYPE.PAY_CHANNEL_CODE"
                :value="detailData.channelCode"
                class="channel-tag"
              />
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="支付 IP">
            <div class="desc-value">
              <Icon icon="ep:location" class="value-icon" />
              <span class="value-text ip-text">{{ detailData.userIp || '-' }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="渠道单号">
            <div class="desc-value">
              <Icon icon="ep:link" class="value-icon" />
              <el-tag
                size="mini"
                type="success"
                v-if="detailData.channelOrderNo"
                class="channel-order-tag"
              >
                {{ detailData.channelOrderNo }}
              </el-tag>
              <span v-else class="no-data">-</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="渠道用户">
            <div class="desc-value">
              <Icon icon="ep:user" class="value-icon" />
              <span class="value-text">{{ detailData.channelUserId || '-' }}</span>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="退款金额">
            <div class="desc-value">
              <Icon icon="ep:refresh-left" class="value-icon refund-icon" />
              <el-tag size="mini" type="danger" class="refund-tag">
                ￥{{ (detailData.refundPrice / 100.0).toFixed(2) }}
              </el-tag>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="通知 URL">
            <div class="desc-value">
              <Icon icon="ep:link" class="value-icon" />
              <span class="value-text url-text">{{ detailData.notifyUrl || '-' }}</span>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 回调信息 -->
      <div class="detail-section">
        <div class="section-title">
          <Icon icon="ep:connection" class="section-icon" />
          <span>回调信息</span>
        </div>

        <el-descriptions
          :column="1"
          label-class-name="desc-label"
          direction="vertical"
          border
          class="enhanced-descriptions callback-descriptions"
        >
          <el-descriptions-item label="支付通道异步回调内容">
            <div class="callback-content">
              <Icon icon="ep:document" class="callback-icon" />
              <div class="callback-text">
                <el-text class="callback-data">{{
                  detailData.extension.channelNotifyData || '暂无回调数据'
                }}</el-text>
              </div>
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import * as OrderApi from '@/api/pay/order'
import { formatDate } from '@/utils/formatTime'

defineOptions({ name: 'PayOrderDetail' })

const dialogVisible = ref(false) // 弹窗的是否展示
const detailLoading = ref(false) // 表单的加载中
const detailData = ref({
  extension: {}
})

/** 打开弹窗 */
const open = async (id: number) => {
  dialogVisible.value = true
  // 设置数据
  detailLoading.value = true
  try {
    detailData.value = await OrderApi.getOrderDetail(id)
    if (!detailData.value.extension) {
      detailData.value.extension = {}
    }
  } finally {
    detailLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 详情容器 */
.detail-container {
  padding: 24px;
  background: white;
  max-height: 70vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    max-height: 60vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

.detail-section {
  margin-bottom: 32px;

  &:last-child {
    margin-bottom: 0;
  }

  @media (max-width: 768px) {
    margin-bottom: 24px;
  }
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid $primary-light;
  font-size: 16px;
  font-weight: 600;
  color: $text-primary;

  @media (max-width: 768px) {
    font-size: 14px;
    margin-bottom: 16px;
    padding-bottom: 8px;
  }
}

.section-icon {
  color: $primary-color;
  font-size: 18px;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

/* 描述列表样式 */
.enhanced-descriptions {
  :deep(.el-descriptions__header) {
    margin-bottom: 16px;
  }

  :deep(.el-descriptions__body) {
    background: #fafbff;
    border-radius: $border-radius-small;
    padding: 16px;
    border: 1px solid $border-light;

    @media (max-width: 768px) {
      padding: 12px;
    }
  }

  :deep(.el-descriptions__table) {
    border-collapse: separate;
    border-spacing: 0;
  }

  :deep(.el-descriptions__cell) {
    padding: 12px 16px;
    border: none;

    &:last-child {
      border-bottom: none;
    }

    @media (max-width: 768px) {
      padding: 8px 12px;
      display: block;
      width: 100% !important;
    }
  }

  :deep(.el-descriptions__label) {
    font-weight: 600;
    color: $text-regular;
    background: rgba(0, 82, 204, 0.05);
    border-radius: 4px;
    padding: 8px 12px;
    font-size: 13px;
    min-width: 120px;

    @media (max-width: 768px) {
      font-size: 12px;
      padding: 6px 8px;
      margin-bottom: 4px;
      display: block;
      width: 100%;
    }
  }

  :deep(.el-descriptions__content) {
    color: $text-primary;
    padding: 8px 12px;

    @media (max-width: 768px) {
      padding: 4px 8px;
    }
  }

  // H5适配：单列显示
  @media (max-width: 768px) {
    :deep(.el-descriptions__table) {
      display: block;
    }

    :deep(.el-descriptions__row) {
      display: block;
    }

    :deep(.el-descriptions__cell) {
      display: block !important;
      width: 100% !important;
    }
  }
}

/* 描述值样式 */
.desc-value {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
  @media (max-width: 768px) {
    gap: 6px;
  }
}

.value-icon {
  font-size: 14px;
  color: $text-secondary;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }

  &.price-icon {
    color: $success-color;
  }

  &.fee-icon {
    color: $warning-color;
  }

  &.refund-icon {
    color: $danger-color;
  }

  &.success-icon {
    color: $success-color;
  }

  &.warning-icon {
    color: $warning-color;
  }
}

.value-text {
  color: $text-primary;
  font-size: 13px;
  line-height: 1.4;

  @media (max-width: 768px) {
    font-size: 12px;
  }

  &.time-text {
    font-family: 'Courier New', monospace;
    font-size: 12px;
    color: $text-secondary;

    @media (max-width: 768px) {
      font-size: 11px;
    }
  }

  &.ip-text {
    font-family: 'Courier New', monospace;
    color: $info-color;
  }

  &.url-text {
    word-break: break-all;
    color: $primary-color;
    font-size: 12px;

    @media (max-width: 768px) {
      font-size: 11px;
    }
  }
}

.no-data {
  color: $text-placeholder;
  font-style: italic;
  font-size: 12px;
}

/* 标签样式 */
.merchant-tag {
  background: $primary-light;
  color: $primary-color;
  border: 1px solid $primary-border;
  font-family: 'Courier New', monospace;
  font-weight: 500;
}

.pay-tag {
  background: rgba(230, 162, 60, 0.1);
  color: $warning-color;
  border: 1px solid rgba(230, 162, 60, 0.3);
  font-family: 'Courier New', monospace;
  font-weight: 500;
}

.price-tag {
  background: rgba(103, 194, 58, 0.1);
  color: $success-color;
  border: 1px solid rgba(103, 194, 58, 0.3);
  font-weight: 600;
  font-size: 13px;
}

.fee-tag {
  background: rgba(230, 162, 60, 0.1);
  color: $warning-color;
  border: 1px solid rgba(230, 162, 60, 0.3);
  font-weight: 500;
}

.refund-tag {
  background: rgba(245, 108, 108, 0.1);
  color: $danger-color;
  border: 1px solid rgba(245, 108, 108, 0.3);
  font-weight: 500;
}

.channel-order-tag {
  background: rgba(103, 194, 58, 0.1);
  color: $success-color;
  border: 1px solid rgba(103, 194, 58, 0.3);
  font-family: 'Courier New', monospace;
  font-size: 11px;
}

.status-tag,
.channel-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
}

/* 回调信息样式 */
.callback-descriptions {
  :deep(.el-descriptions__body) {
    background: #f8f9fa;
    border: 1px solid $border-light;
  }

  :deep(.el-descriptions__label) {
    background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
    color: white;
    font-weight: 600;
  }
}

.callback-content {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  background: white;
  border-radius: $border-radius-small;
  border: 1px solid $border-light;

  @media (max-width: 768px) {
    gap: 8px;
    padding: 8px;
  }
}

.callback-icon {
  color: $primary-color;
  font-size: 16px;
  flex-shrink: 0;
  margin-top: 2px;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.callback-text {
  flex: 1;
  min-width: 0;
}

.callback-data {
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.6;
  color: $text-regular;
  word-break: break-all;
  white-space: pre-wrap;
  background: #f8f9fa;
  padding: 8px 12px;
  border-radius: 4px;
  border: 1px solid $border-light;
  display: block;
  max-height: 200px;
  overflow-y: auto;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 6px 8px;
    max-height: 150px;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 2px;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .detail-container {
    background: #374151;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-descriptions {
    :deep(.el-descriptions__body) {
      background: #4b5563;
      border-color: #6b7280;
    }

    :deep(.el-descriptions__label) {
      background: rgba(96, 165, 250, 0.1);
      color: #f9fafb;
    }

    :deep(.el-descriptions__content) {
      color: #d1d5db;
    }

    :deep(.el-descriptions__cell) {
      border-color: #6b7280;
    }
  }

  .value-text {
    color: #d1d5db;

    &.url-text {
      color: #60a5fa;
    }
  }

  .merchant-tag {
    background: rgba(96, 165, 250, 0.1);
    color: #60a5fa;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .callback-content {
    background: #4b5563;
    border-color: #6b7280;
  }

  .callback-data {
    background: #374151;
    border-color: #6b7280;
    color: #d1d5db;
  }

  .callback-descriptions {
    :deep(.el-descriptions__body) {
      background: #374151;
    }

    :deep(.el-descriptions__label) {
      background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
      color: white;
    }
  }
}

/* 兼容旧样式 */
.tag-purple {
  color: #722ed1;
  background: #f9f0ff;
  border-color: #d3adf7;
}

.tag-pink {
  color: #eb2f96;
  background: #fff0f6;
  border-color: #ffadd2;
}
</style>
